package com.base.vistter.iframe.utils;

import com.base.vistter.iframe.constant.CommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Slf4j
@Component
public class RedisUtils {

    private static RedisTemplate<String, Object> redisTemplate;

    public static boolean hasKey(String key) {
        return redisTemplate.hasKey(key);
    }

    public static Set<String> keys(String keyMatch) {
        return redisTemplate.keys(keyMatch);
    }

    public static void setString(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public static void setString(String key, String value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    public static String getString(String key) {
        if (hasKey(key)) {
            Object value = redisTemplate.opsForValue().get(key);
            return String.valueOf(value);
        }
        return null;
    }

    public static void setInteger(String key, Integer value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public static void setInteger(String key, Integer value, long timeout, TimeUnit unit) {
        redisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    public static void setList(String key, List<Object> list) {
        redisTemplate.opsForList().rightPush(key, list);
    }

    public static List<Object> getList(String key) {
        if (hasKey(key)) {
            return redisTemplate.opsForList().range(key, 0, -1);
        }
        return null;
    }

    public static <T> Long getListSize(String key) {
        return redisTemplate.opsForList().size(key);
    }

    /**
     * redis获取整形
     */
    public static Integer getInteger(String key) {
        if (hasKey(key)) {
            Object value = redisTemplate.opsForValue().get(key);
            return Integer.parseInt(String.valueOf(value));
        }
        return 0;

    }

    public static void deleteKeyMatch(String keyMatch) {
        Set<String> keys = keys(keyMatch + "*");
        keys.forEach(RedisUtils::delete);
    }

    public static Boolean delete(String key) {
        if (!hasKey(key)) {
            return false;
        }
        return redisTemplate.delete(key);
    }

    public Long delete(Collection<String> keys) {
        return redisTemplate.delete(keys);
    }

    /**
     * 设置token
     */
    public static void setToken(String token, long timeout, TimeUnit unit) {
        setString(CommonConstant.PREFIX_USER_TOKEN + token, token, timeout, unit);
    }

    public static void setNewToken(String token, String newToken, long timeout, TimeUnit unit) {
        setString(CommonConstant.PREFIX_USER_TOKEN + token, newToken, timeout, unit);
    }

    /**
     * 获取缓存中的token
     */
    public static String getToken(String token) {
        if (StringUtils.isNotEmpty(token)) {
            return RedisUtils.getString(CommonConstant.PREFIX_USER_TOKEN + token);
        }
        return null;
    }

    public static void removeToken(String token) {
        delete(CommonConstant.PREFIX_USER_TOKEN + token);
    }

    @Autowired
    private void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        RedisUtils.redisTemplate = redisTemplate;
    }

}
